package com.okjava.web;

import com.alibaba.fastjson.JSONArray;
import com.okjava.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Description:
 * Created @version 1.0    2016/9/22 14:25 by Benny
 */
@Controller
@RequestMapping("/")
public class ThemeController {

    @RequestMapping(value = "/theme", method = RequestMethod.GET)
    @ResponseBody
    public String getThemeNames(HttpServletRequest request) {
        // 获得项目的路径
        ServletContext sc = request.getSession().getServletContext();
        String path = getClass().getResource("/static/images/bg/").getPath();
        File file = new File(path);
        String realPath = file.getPath();
        JSONArray array = new JSONArray();
        try {
            Files.list(Paths.get(realPath)).map(path1 -> {
                String[] split = path1.toString().split("\\\\");
                return split[split.length - 1];
            }).forEach(array::add);
            return ResponseUtil.handleSuccess(array);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseUtil.handleFailed("转换失败");
    }

}
